﻿
Partial Class pages_products_usercontrols_UCProductSelect
    Inherits System.Web.UI.UserControl

    Private m_blankItem As String = "-- Any Product --"
    Public Property BlankItem As String
        Get
            Return m_blankItem
        End Get
        Set(value As String)
            m_blankItem = value
        End Set
    End Property

    Private m_transcriptsOnly As Boolean = False
    Public Property TranscriptsOnly As Boolean
        Get
            Return m_transcriptsOnly
        End Get
        Set(value As Boolean)
            m_transcriptsOnly = value
        End Set
    End Property

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim id As Integer = CWeb.RequestInt("productId")
        Dim p As CProduct = CProduct.Cache.GetById(id)

        Dim list As CProductList = CProduct.Cache
        If Not IsNothing(p) Then
            If Not IsNothing(p.BelongsTo) Then
                list = p.BelongsTo.Children
            Else
                list = CProduct.Cache.GetByTypeId(p.ProductTypeId_)
            End If
        End If
        If TranscriptsOnly Then list = CProduct.Cache.HavingTranscripts
        ddProduct.DataSource = list
        ddProduct.DataBind()
        If Not IsNothing(p) AndAlso IsNothing(list.GetById(id)) Then CDropdown.Add(ddProduct, p.ProductName, id)
        CDropdown.BlankItem(ddProduct, BlankItem)
        CDropdown.SetValue(ddProduct, id)

        If IsNothing(p) Then
            lnkPreview.Visible = False
            lnkEbook.Visible = False
        Else
            lnkPreview.NavigateUrl = CSitemap.Decide(p)
            If p.HasParent Then
                p = p.BelongsTo
                lnkEbook.NavigateUrl = "~/pages/ebooks/generate.aspx?productId=" & p.ProductId
            ElseIf True OrElse p.Children.Count > 0 Then
                lnkEbook.NavigateUrl = "~/pages/ebooks/generate.aspx?productId=" & p.ProductId
            Else
                lnkEbook.NavigateUrl = "~/transcript.aspx?productId=" & p.ProductId & "&html=true"
            End If
        End If
    End Sub


    Protected Sub ddProduct_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddProduct.SelectedIndexChanged
        Dim url As String = Request.RawUrl
        url = url.Substring(0, url.IndexOf("productId=") + 10) + ddProduct.SelectedValue
        Response.Redirect(url)
    End Sub
End Class
